﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTreeList;
using System.Data;
using olierFrm.WebFormDev;
using olierFrm.WebFormDev.Session;
using olierFrm.DataBase.FrameWork.StdQuery;


namespace WebFormDev
{
    public partial class Menu : System.Web.UI.Page
    {

        protected void Page_PreInit(object sender, EventArgs e)
        {
            //ManageSystem.IsLogin();
            ManageSystem.SetTheme(Page);
            //没有QueryName只显示菜单，为了重复利用该页面（系统首页和StdQuery）

        }

        protected string treeStyle;
        protected void Page_Load(object sender, EventArgs e)
        {
            treeStyle = "<style>.dxtlNode_" + Page.Theme + "{background-color: transparent;}.dxtlIndent_" + Page.Theme + ", .dxtlIndentWithButton_" + Page.Theme + "{background-color: transparent;}</style>";
            CreateTree();
            ASPxTreeList1.Border.BorderStyle = BorderStyle.None;
        }

        private void BindMenu(DataTable treeTable, TreeListNode miParent, string parentMenuID)
        {
            DataView dv = new DataView(treeTable);
            dv.RowFilter = "ParentMenu = '" + parentMenuID + "'";
            var dt = dv.ToTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeListNode node;
                if (miParent != null)
                    node = CreateNodeCore(dt.Rows[i]["ModuleID"].ToString(), dt.Rows[i]["ModuleName"].ToString(), dt.Rows[i]["WebURL"].ToString(), miParent);
                else
                    node = CreateNodeCore(dt.Rows[i]["ModuleID"].ToString(), dt.Rows[i]["ModuleName"].ToString(), dt.Rows[i]["WebURL"].ToString(), null);
                BindMenu(treeTable, node, dv[i]["MenuTree"].ToString());
            }
        }

        void CreateTree()
        {
            if (SSession.Menu == null)
            {
                var su = ((SUser)Session[SUser.SessionID]);
                StdQuerySQL sqs = new StdQuerySQL("Menus");
                sqs.SetParam("UserID", su.Operator);
                sqs.SetParam("StoreNO", "001");
                sqs.SetParam("IsWeb", "1");
                SSession.Menu = new SMenu();
                SSession.Menu.DataSource = sqs.ExecuteSQL();
            }
            ASPxTreeList1.SettingsText.LoadingPanelText = "正在打开，请稍候。";
            ASPxTreeList1.ClientSideEvents.EndCallback = "function(s,e){$(\"tr[id*='ASPxTreeList1_R'],td[class*='dxtlIndentWithButton'],td[class*='dxtlIndent']\").css(\"background-color\", \"transparent\");}";
            ASPxTreeList1.ClientSideEvents.BeginCallback = "function(s,e){$(\"tr[id*='ASPxTreeList1_R'],td[class*='dxtlIndentWithButton'],td[class*='dxtlIndent']\").css(\"background-color\", \"transparent\");}";
            ASPxTreeList1.Settings.ShowColumnHeaders = false;
            //ASPxTreeList1.ClearNodes();
            //ASPxTreeList1.Columns.Clear();
            ASPxTreeList1.EncodeHtml = false;
            ASPxTreeList1.DataSource = SSession.Menu.DataSource;
            ASPxTreeList1.DataBind();
            //TreeListDataColumn tlc = new TreeListDataColumn("菜单");
            //TreeListDataColumn tlc2 = new TreeListDataColumn("url");

            //ASPxTreeList1.Columns.Add(tlc);
            //ASPxTreeList1.Columns.Add(tlc2);

            //BindMenu(SessionMenu.Session.DataSource, null, "0");
        }
        TreeListNode CreateNodeCore(string key, string text, string url, TreeListNode parentNode)
        {
            TreeListNode node = ASPxTreeList1.AppendNode(key, parentNode);
            node["菜单"] = text;
            return node;
        }
    }
}